package arkham.data;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import arkham.data.Globals.DimensionalSymbol;
import arkham.jaxb.encounter.GateTileType;
import arkham.util.AHLogger;
import arkham.util.ImageUtils;
import arkham.util.XmlTools;

public class GateTile implements NamedEntity
{
	
	private String name;
	private String description;
	private DimensionalSymbol symbol;
	private int modifier;
	private BufferedImage image;
	
	public GateTile(String name)
	{
		this.name = name;
	}

	/**
	 * Gets the name of the other world location
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the text displayed on the GUI for the other world location
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Gets the dimensional symbol
	 * @return the symbol
	 */
	public DimensionalSymbol getSymbol() {
		return symbol;
	}
	
	/**
	 * Gets the image of this gate tile
	 * @return the image
	 */
	public BufferedImage getImage()
	{
		return image;
	}
	
	/**
	 * Creates the image of this gate tile
	 * from the data
	 */
	public void createImage()
	{
		if (symbol == null) return;
		BufferedImage openGate = ImageUtils.loadImage(
				ResourceSet.getInstance().getIconFilename("Gate Tile"));
		image = new BufferedImage(45, 45, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = image.createGraphics();
		g.drawImage(openGate, 0, 0, null);
		Font narrowFont = ImageUtils.loadFont("fonts/bluecond.ttf").deriveFont(
				Font.PLAIN, 14);
		g.setFont(narrowFont);
		
		FontMetrics fm = g.getFontMetrics();
		Rectangle2D bounds = fm.getStringBounds(description, g);
		int tx = (int)Math.round(22 - bounds.getWidth() / 2);
		int ty = (int)Math.round(8 - bounds.getHeight() / 2);
		g.setColor(new Color(233, 233, 192));
		g.fillRect(tx, ty, (int)Math.round(bounds.getWidth()), (int)Math.round(bounds.getHeight()));
		g.setColor(Color.black);
		g.drawString(description, tx, ty + fm.getAscent());
		
		g.setColor(Color.red);
		g.setFont(narrowFont.deriveFont(Font.BOLD, 14));
		g.drawImage(symbol.getImage(), 17, 35, null);
		g.drawString(XmlTools.getModifier(modifier), 32, 27);
		g.dispose();
	}

	/**
	 * Gets the gate closing modifier
	 * @return the modifier
	 */
	public int getModifier() {
		return modifier;
	}
	
	@Override
	public String toString()
	{
		return name;
	}
	
	/**
	 * Creates the data representation of the gate tile from the XML info
	 * and performs a stringent validation check to make sure the XML data is
	 * valid. 
	 * <br>Will return <b>null</b> on a failure.
	 * 
	 * @param xmlmyth the XML gate tile info
	 * @return the data representation of the gate tile
	 */
	public static GateTile createGateTileFromXml(GateTileType xmlgatetile)
	{
		GateTile gt = new GateTile(xmlgatetile.getName());
		gt.description = xmlgatetile.getDescription();
		gt.modifier = xmlgatetile.getModifier();
		if (!DimensionalSymbol.isValidSymbol(xmlgatetile.getSymbol()))
		{
			AHLogger.error("Gate tile " + gt.name + " has invalid dimensional symbol.");
			return null;
		}
		gt.symbol = DimensionalSymbol.getDimensionalSymbol(xmlgatetile.getSymbol());
		gt.createImage();
		return gt;
	}
}
